package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendShiftScheduleCycleDAO;
import org.jsola.hr.dto.AttendShiftScheduleCycleAddDTO;
import org.jsola.hr.dto.AttendShiftScheduleCycleUpdateDTO;
import org.jsola.hr.entity.AttendShiftScheduleCycleDO;
import org.jsola.hr.query.AttendShiftScheduleCycleQuery;
import org.jsola.hr.service.IAttendShiftScheduleCycleService;
import org.jsola.hr.vo.AttendShiftScheduleCycleListVO;
import org.jsola.hr.vo.AttendShiftScheduleCycleVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendShiftScheduleCycleServiceImpl")
public class AttendShiftScheduleCycleServiceImpl implements IAttendShiftScheduleCycleService {

    @Autowired
    private IAttendShiftScheduleCycleDAO attendShiftScheduleCycleDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendShiftScheduleCycleVO save(AttendShiftScheduleCycleAddDTO attendShiftScheduleCycleAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendShiftScheduleCycleDO attendShiftScheduleCycleDO = attendShiftScheduleCycleAddDTO.to(AttendShiftScheduleCycleDO.class);
        // 保存
        attendShiftScheduleCycleDO = save(attendShiftScheduleCycleDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendShiftScheduleCycleDO.to(AttendShiftScheduleCycleVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendShiftScheduleCycleUpdateDTO attendShiftScheduleCycleUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendShiftScheduleCycleDO attendShiftScheduleCycleDO = attendShiftScheduleCycleUpdateDTO.to(AttendShiftScheduleCycleDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendShiftScheduleCycleDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendShiftScheduleCycleIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendShiftScheduleCycleIds);
    }


    @Override
    public AttendShiftScheduleCycleVO selectById(String attendShiftScheduleCycleId, String siteId) {
        AttendShiftScheduleCycleDO attendShiftScheduleCycleDO = selectDOById(attendShiftScheduleCycleId, siteId);
        if (attendShiftScheduleCycleDO == null) {
            return null;
        }
        return attendShiftScheduleCycleDO.to(AttendShiftScheduleCycleVO.class);
    }

    @Override
    public List<AttendShiftScheduleCycleListVO> select(AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery, String siteId) {
        List<AttendShiftScheduleCycleDO> attendShiftScheduleCycleDOList = selectDO(attendShiftScheduleCycleQuery, siteId);
        if(CollectionUtils.isEmpty(attendShiftScheduleCycleDOList)) {
            return attendShiftScheduleCycleDOList == null ? null : new ArrayList<>();
        }
        return attendShiftScheduleCycleDOList.stream()
                .map(attendShiftScheduleCycleDO -> attendShiftScheduleCycleDO.to(AttendShiftScheduleCycleListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery, String siteId) {
        Example example = buildExample(attendShiftScheduleCycleQuery,siteId);
        return attendShiftScheduleCycleDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendShiftScheduleCycleListVO> selectPage(AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery, String siteId) {
        Example example = buildExample(attendShiftScheduleCycleQuery,siteId);
        Page<AttendShiftScheduleCycleDO> page = attendShiftScheduleCycleDAO.selectPageByExample(example,
                attendShiftScheduleCycleQuery.getPageNo(),
                attendShiftScheduleCycleQuery.getPageSize());

        return page.to(AttendShiftScheduleCycleListVO.class);
    }

    @Override
    public AttendShiftScheduleCycleDO selectDOById(String attendShiftScheduleCycleId, String siteId) {
        return listById(attendShiftScheduleCycleId, siteId);
    }

    @Override
    public List<AttendShiftScheduleCycleDO> selectDO(AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery, String siteId) {
        Example example = buildExample(attendShiftScheduleCycleQuery,siteId);
        return attendShiftScheduleCycleDAO.selectByExample(example);
    }

    @Override
    public int deleteByAttendPlanId(String attendPlanId, TokenUser tokenUser) {
        AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery = new AttendShiftScheduleCycleQuery();
        attendShiftScheduleCycleQuery.setAttendPlanId(attendPlanId);
        Example example = buildExample(attendShiftScheduleCycleQuery, tokenUser.getSiteId());
        return attendShiftScheduleCycleDAO.deleteByExample(example);
    }

    @Override
    public int updateAttendPlanId(String tmpAttendPlanId, String attendPlanId, TokenUser tokenUser) {

        AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery = new AttendShiftScheduleCycleQuery();
        attendShiftScheduleCycleQuery.setAttendPlanId(attendPlanId);
        Example example = buildExample(attendShiftScheduleCycleQuery, tokenUser.getSiteId());

        AttendShiftScheduleCycleDO record = new AttendShiftScheduleCycleDO();
        record.setAttendPlanId(attendPlanId);

        return attendShiftScheduleCycleDAO.updateByExampleSelective(record, example);
    }

    @Override
    public int logicDelByAttendPlanId(String attendPlanId, TokenUser tokenUser) {

        // 查询
        AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery = new AttendShiftScheduleCycleQuery();
        attendShiftScheduleCycleQuery.setAttendPlanId(attendPlanId);
        List<AttendShiftScheduleCycleDO> dataList = this.selectDO(attendShiftScheduleCycleQuery, tokenUser.getSiteId());
        if (dataList.size() <= 0) {
            return 0;
        }

        for (AttendShiftScheduleCycleDO attendShiftScheduleCycle : dataList) {

            AttendShiftScheduleCycleDO attendShiftScheduleCycleDO = new AttendShiftScheduleCycleDO();

            attendShiftScheduleCycleDO.setId(attendShiftScheduleCycle.getId());
            attendShiftScheduleCycleDO.setValid(false);
            attendShiftScheduleCycleDO.setModifiedUserId(tokenUser.getUserId());
            attendShiftScheduleCycleDO.setGmtModified(new Date());

            attendShiftScheduleCycleDAO.updateByPrimaryKeySelective(attendShiftScheduleCycleDO);
        }

        return 0;
    }

    /**
     * 根据查询参数，构建example

     * @param attendShiftScheduleCycleQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendShiftScheduleCycleQuery attendShiftScheduleCycleQuery, String siteId) {
        Example example = new Example(AttendShiftScheduleCycleDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendShiftScheduleCycleQuery.getAttendPlanId() != null) {
            example.and().andLike("attendPlanId", "%" + attendShiftScheduleCycleQuery.getAttendPlanId() + "%");
        }
        if (attendShiftScheduleCycleQuery.getName() != null) {
            example.and().andLike("name", "%" + attendShiftScheduleCycleQuery.getName() + "%");
        }
        if (attendShiftScheduleCycleQuery.getDaysJson() != null) {
            example.and().andLike("daysJson", "%" + attendShiftScheduleCycleQuery.getDaysJson() + "%");
        }
        if (attendShiftScheduleCycleQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", attendShiftScheduleCycleQuery.getCompanyId());
        }
        if (attendShiftScheduleCycleQuery.getGmtCreateStart() != null &&
                attendShiftScheduleCycleQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendShiftScheduleCycleQuery.getGmtCreateStart(),
                    attendShiftScheduleCycleQuery.getGmtCreateEnd());
        }
        if (attendShiftScheduleCycleQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendShiftScheduleCycleQuery.getCreateUserId() + "%");
        }
        if (attendShiftScheduleCycleQuery.getGmtModifiedStart() != null &&
                attendShiftScheduleCycleQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendShiftScheduleCycleQuery.getGmtModifiedStart(),
                    attendShiftScheduleCycleQuery.getGmtModifiedEnd());
        }
        if (attendShiftScheduleCycleQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendShiftScheduleCycleQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendShiftScheduleCycleQuery.getOrders());
        return example;
    }
}




